package com.jbinfo.mongoot.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jbinfo.mongoot.operation.Volunteer.Volunteer;
import com.jbinfo.mongoot.operation.oldReturnVist.OldReturnVist;
import com.jbinfo.mongoot.operation.servant.Servant;
import com.jbinfo.mongoot.operation.visitor.Visitor;
import com.jbinfo.mongoot.util.PageModel;
import com.jbinfo.mongoot.util.XiMethodUtil;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

@RestController
@RequestMapping("/servant")
@Api("fuzhou-servant类API")
public class ServantController {
	@Autowired
	private MongoTemplate mongoTemplate;
	
	@ApiOperation("获得所有的ServantList")
	@RequestMapping(value = "/getAll", method = RequestMethod.GET)
	public Map<String, Object> getAll() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("status", 1);
		map.put("description", "");
		map.put("code", "");
		List<Servant> list = mongoTemplate.find(new Query(), Servant.class);
		map.put("result", list);
		return map;
	}
	
	@ApiOperation("按条件查询servant(姓名员工编号登录名身份证号在职状态电话号码服务分类（第三类别）)")
	@RequestMapping(value = "/searchByConditions", method = RequestMethod.GET)
	public Map<String, Object> searchByConditions(@ModelAttribute Servant servant){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("status", 1);
		map.put("description", "");
		map.put("code", "");
		Query query=new Query();
	    Criteria criatira = new Criteria();
	    if(!StringUtils.isEmpty(servant.getCompany_code())){
			criatira.andOperator(Criteria.where("company_code").regex(".*?" +servant.getCompany_code()+ ".*"));
		}
		if(!StringUtils.isEmpty(servant.getName())){
			criatira.andOperator(Criteria.where("name").regex(".*?" +servant.getName()+ ".*"));
		}
		if(!StringUtils.isEmpty(servant.getCode())){
			   criatira.andOperator(Criteria.where("code").is(servant.getCode())); 
		}
		if(!StringUtils.isEmpty(servant.getLoginName())){
			   criatira.andOperator(Criteria.where("loginName").is(servant.getLoginName())); 
		}
		if(!StringUtils.isEmpty(servant.getCardId())){
			   criatira.andOperator(Criteria.where("cardId").is(servant.getCardId())); 
		}
		if(!StringUtils.isEmpty(servant.getStatus())){
			   criatira.andOperator(Criteria.where("status").is(servant.getStatus())); 
		}
	    if(!StringUtils.isEmpty(servant.getPhone())){
		   criatira.andOperator(Criteria.where("phone").is(servant.getPhone())); 
	    }
	    if(!StringUtils.isEmpty(servant.getService3_code())){
			   criatira.andOperator(Criteria.where("service3_code").is(servant.getService3_code())); 
		}
	   query.addCriteria(criatira);
	   List<Servant> list=mongoTemplate.find(query,Servant.class); 
	   map.put("list", list);
	   map.put("result", map);
	   return map;
	}
	
	@ApiOperation("按条件分页查询servant(姓名员工编号登录名身份证号在职状态电话号码服务分类（第三类别）)")
	@RequestMapping(value = "/searchByConditionsPage", method = RequestMethod.GET)
	public Map<String, Object> searchByConditionsPage(@ModelAttribute PageModel<Servant> page,@ModelAttribute Servant servant){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("status", 1);
		map.put("description", "");
		map.put("code", "");
		Query query=new Query();
	    Criteria criatira = new Criteria();
	    if(!StringUtils.isEmpty(servant.getCompany_code())){
			criatira.andOperator(Criteria.where("company_code").regex(".*?" +servant.getCompany_code()+ ".*"));
		}
		if(!StringUtils.isEmpty(servant.getName())){
			criatira.andOperator(Criteria.where("name").regex(".*?" +servant.getName()+ ".*"));
		}
		if(!StringUtils.isEmpty(servant.getCode())){
			   criatira.andOperator(Criteria.where("code").is(servant.getCode())); 
		}
		if(!StringUtils.isEmpty(servant.getLoginName())){
			   criatira.andOperator(Criteria.where("loginName").is(servant.getLoginName())); 
		}
		if(!StringUtils.isEmpty(servant.getCardId())){
			   criatira.andOperator(Criteria.where("cardId").is(servant.getCardId())); 
		}
		if(!StringUtils.isEmpty(servant.getStatus())){
			   criatira.andOperator(Criteria.where("status").is(servant.getStatus())); 
		}
	    if(!StringUtils.isEmpty(servant.getPhone())){
		   criatira.andOperator(Criteria.where("phone").is(servant.getPhone())); 
	    }
	    if(!StringUtils.isEmpty(servant.getService3_code())){
			   criatira.andOperator(Criteria.where("service3_code").is(servant.getService3_code())); 
		}
	   query.addCriteria(criatira);
	   //查询总数  
	   int count=(int) mongoTemplate.count(query,Servant.class);  
	   page.setTotal(count);
	   //排序  
	   query.with(new Sort(Direction.ASC, "code"));  
	   query.skip(page.getSkip()).limit(page.getPageSize());  
	   List<Servant>datas=mongoTemplate.find(query,Servant.class);  
	   page.setRows(datas);
	   map.put("result", page);
	   return map;
	}
	
	@ApiOperation("新增或更新servant")
	@RequestMapping(value = "/addOrUpdate", method = RequestMethod.POST)
	public Map<String, Object> addOrUpdate(@ModelAttribute Servant servant) throws ParseException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("status", 1);
		map.put("description", "");
		map.put("code", "");
		if("".equals(servant.getId()) ||  servant.getId() == null){
			Date currentTime = new Date();
		    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			servant.setCreate_time(formatter.format(currentTime));//创建时间
			mongoTemplate.insert(servant);
		}else{
			DBCollection collection = mongoTemplate.getCollection(mongoTemplate.getCollectionName(servant.getClass()));
			DBObject query =new BasicDBObject();
			query.put("code", servant.getCode());
			DBObject findOne = collection.findOne(query);
			DBObject update=XiMethodUtil.method(servant,findOne);
			collection.update(query, update, true, true);
		}
		return map;
	}
	
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public Map<String, Object> delete(@RequestParam("id") String id) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("status", 1);
		map.put("description", "");
		map.put("code", "");
		DBCollection collection = mongoTemplate.getCollection(mongoTemplate.getCollectionName(Servant.class));
		DBObject query =new BasicDBObject();
		query.put("_id", id);
		DBObject update = collection.findOne(query);
		update.put("invalid", "0");
		collection.update(query, update, false, true);
		return map;
    }
	
	
}
